from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import DetailView, ListView, UpdateView
from django.views.generic.edit import CreateView

from .models import Product

# Create your views here.


class ProductListView(LoginRequiredMixin, ListView):
    model = Product

    # product_list.html


class ProductFormView(LoginRequiredMixin, CreateView):
    model = Product
    fields = ["name", "description", "server"]

    def form_valid(self, form):
        form.instance.created_user = self.request.user
        return super().form_valid(form)


class ProductDetailView(LoginRequiredMixin, DetailView):
    model = Product


class ProductUpdateView(LoginRequiredMixin, UpdateView):
    model = Product
    fields = [
        "name",
        "description",
    ]
    template_name = "product/product_form.html"

    def form_valid(self, form):
        form.instance.created_user = self.request.user
        return super().form_valid(form)
